#include <iostream>
#include <filesystem>
#include <cstdlib>
#include <getopt.h>

namespace fs = std::filesystem;

void usage(int status) {
    std::cerr << "\033[32m" << "用法: mkdir [选项]... 目录..." << "\033[0m" << std::endl;
    std::cerr << "创建目录，如果它们尚不存在。\n\n";
    std::cerr << "\033[32m" << "选项:" << "\033[0m" << std::endl;
    std::cerr << "  -p, --make-ancestor=DIR  创建缺失的祖先目录\n";
    std::cerr << "  -m, --mode=MODE          设置文件模式（如chmod命令），不是a=rwx - umask\n";
    std::cerr << "  -v, --verbose            为每个创建的目录打印一条消息\n";
    std::cerr << "  -Z, --context=CONTEXT    设置SELinux安全上下文\n";
    std::cerr << "  -h, --help               显示此帮助并退出\n";
    std::exit(status);
}

void create_directory(const std::string& path, std::filesystem::perms permissions, bool verbose) {
    try {
        fs::create_directory(path);
        if (verbose) {
            std::cout << "mkdir: created directory '" << path << "'" << std::endl;
        }
    } catch (const std::filesystem::filesystem_error& ex) {
        std::cerr << "mkdir: " << ex.what() << std::endl;
        std::exit(EXIT_FAILURE);
    }
}

void make_ancestor(const std::string& path, std::filesystem::perms permissions, bool verbose) {
    fs::path dir_path = path;
    try {
        fs::create_directories(dir_path);
        if (verbose) {
            std::cout << "mkdir: created directory '" << path << "'" << std::endl;
        }
    } catch (const std::filesystem::filesystem_error& ex) {
        std::cerr << "mkdir: " << ex.what() << std::endl;
        std::exit(EXIT_FAILURE);
    }
}

int main(int argc, char *argv[]) {
    int optc;
    std::string specified_mode;
    std::filesystem::perms permissions = fs::perms::owner_all | fs::perms::group_read | fs::perms::others_read;
    bool verbose = false;

    static struct option longopts[] = {
        {"make-ancestor", required_argument, NULL, 'p'},
        {"mode", required_argument, NULL, 'm'},
        {"verbose", no_argument, NULL, 'v'},
        {"context", required_argument, NULL, 'Z'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };

    while ((optc = getopt_long(argc, argv, "p:m:vZ:h", longopts, NULL)) != -1) {
        switch (optc) {
            case 'p':
                // Option -p, --make-ancestor=DIR
                make_ancestor(optarg, permissions, verbose);
                return EXIT_SUCCESS; // Exit after creating ancestors
                break;

            case 'm':
                // Option -m MODE, --mode=MODE
                specified_mode = optarg;
                permissions = fs::perms::all;
                break;

            case 'v':
                // Option -v, --verbose
                verbose = true;
                break;

            case 'Z':
                // Option -Z CONTEXT, --context=CONTEXT
                // Handle SELinux security context here
                std::cerr << "mkdir: option '-Z' is not yet supported" << std::endl;
                return EXIT_FAILURE;

            case 'h':
                // Option --help
                usage(EXIT_SUCCESS);
                break;

            case '?':
            default:
                // Invalid option or missing argument
                usage(EXIT_FAILURE);
                break;
        }
    }

    // Handle non-option arguments (directories to be created)
    for (int i = optind; i < argc; ++i) {
        // Check if the directory already exists
        if (fs::exists(argv[i]) && fs::is_directory(argv[i])) {
            std::cerr << "mkdir: cannot create directory '" << argv[i] << "': File exists" << std::endl;
        } else {
            // Create the directory with specified options
            create_directory(argv[i], permissions, verbose);
        }
    }

    return EXIT_SUCCESS;
}

